package com.eliteams.quick4j.core.util;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * JSON
 * 
 * @author huliang
 * @version $Id: JSONUtil.java, v 0.1 2011-12-12 下午5:12:44 huliang Exp $
 */
public class JSONUtil<T> {

    private static final Logger LOGGER = LoggerFactory.getLogger(JSONUtil.class);

    /**
     * 
     * @param response
     * @param jsonStr
     */
    public static void write(HttpServletResponse response, String jsonStr) {
        try {
            if(LOGGER.isDebugEnabled()){
                LOGGER.debug("JSON Str：" + jsonStr);
            }
            if(StringUtils.isNotBlank(jsonStr)){
                response.setCharacterEncoding("UTF-8");
                PrintWriter writer = response.getWriter();
                writer.print(jsonStr);
                writer.close();
            }
        } catch (IOException e) {
            LOGGER.error("发生异常,详细信息==>>",e);
        }
    }
    
    /**
     * 
     * 
     * @param coll
     * @param size
     * @return
     */
    public String createJsonByCollection(Collection<T> coll,int size){
        JSONObject json = new JSONObject();
        if(coll == null || coll.size() == 0){
            json.put("result", "failure");
            json.put("failure", "true");
            json.put("message", "没有查询到相应的信息");
        }else{
            Iterator<T> iter = coll.iterator();
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObj = null;
            Map<?,?> map = null;
            Set<?> keySet = null;
            Iterator<?> keyIter = null;
            Object key = null;
            try{
                while(iter.hasNext()){
                    map = BeanUtils.describe(iter.next());
                    keySet = map.keySet();
                    keyIter = keySet.iterator();
                    jsonObj = new JSONObject();
                    while(keyIter.hasNext()){
                        key = keyIter.next();
                        jsonObj.put(key, map.get(key));
                    }
                    jsonArray.add(jsonObj);
                }
                json.put("totalProperty",size);
                json.put("root", jsonArray);
            } catch (IllegalAccessException e) {
                json.put("result", "failure");
                json.put("failure", "true");
                json.put("message", e.getMessage());
            } catch (InvocationTargetException e) {
                json.put("result", "failure");
                json.put("failure", "true");
                json.put("message", e.getMessage());
            } catch (NoSuchMethodException e) {
                json.put("result", "failure");
                json.put("failure", "true");
                json.put("message", e.getMessage());
            }
        }
        return json.toString();
    }

}
